package com.pda.common.dialogs.product;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import com.pda.common.R;
import com.pda.common.database.DataBaseControl;
import com.pda.common.database.ProductInfo;
import java.util.ArrayList;
import java.util.List;

public class ProductDialog extends Dialog {
    private String TAG = "ProductDialog";
    private ImageView ivexit;
    private EditText etsn;
    private ListView listView;
    private PriorityListener listener;
    private Context mContext;
    private ProductAdapter productAdapter;
    List<ProductInfo> listt = new ArrayList<>();
    private View layout;

    public interface PriorityListener {
        void setActivityText(ProductInfo productInfo);
    }

    public ProductDialog(final Context context, int theme, PriorityListener priorityListener) {
        super(context, theme);
        mContext = context;
        listener = priorityListener;
        layout = View.inflate(context, R.layout.dialog_product, null);
        setContentView(layout);
        ivexit = (ImageView) layout.findViewById(R.id.ivexit);
        ivexit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        etsn = (EditText) layout.findViewById(R.id.etsn);
        etsn.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if(charSequence.toString().length()!=0)
                {
                    listt= DataBaseControl.getProductNC(charSequence.toString());
                    productAdapter= new ProductAdapter(mContext,listt);
                    listView.setAdapter(productAdapter);
                }
                else
                {
                    listt= DataBaseControl.getProductAll();
                    productAdapter= new ProductAdapter(mContext,listt);
                    listView.setAdapter(productAdapter);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });

        listView = (ListView) layout.findViewById(R.id.listview1);
        View headView = getLayoutInflater().inflate(R.layout.list_producthead, null);
        listView.addHeaderView(headView);
        listt= DataBaseControl.getProductAll();
        productAdapter= new ProductAdapter(mContext,listt);
        listView.setAdapter(productAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position==0)
                {
                    return;
                }
                productAdapter.setSelectedPosition(position-1);
                productAdapter.notifyDataSetInvalidated();
                listener.setActivityText(listt.get(position-1));
                dismiss();
            }
        });
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }
}


